/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.OutBoundOrder;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.OutBoundOrderRepository;
import me.zhengjie.service.OutBoundOrderService;
import me.zhengjie.service.dto.OutBoundOrderDto;
import me.zhengjie.service.dto.OutBoundOrderQueryCriteria;
import me.zhengjie.service.mapper.OutBoundOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-13
**/
@Service
@Transactional
@RequiredArgsConstructor
public class OutBoundOrderServiceImpl implements OutBoundOrderService {

    private final OutBoundOrderRepository outBoundOrderRepository;
    private final OutBoundOrderMapper outBoundOrderMapper;

    @Override
    public PageResult<OutBoundOrderDto> queryAll(OutBoundOrderQueryCriteria criteria, Pageable pageable){
        Page<OutBoundOrder> page = outBoundOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(outBoundOrderMapper::toDto));
    }

    @Override
    public List<OutBoundOrderDto> queryAll(OutBoundOrderQueryCriteria criteria){
        return outBoundOrderMapper.toDto(outBoundOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OutBoundOrderDto findById(Integer id) {
        OutBoundOrder outBoundOrder = outBoundOrderRepository.findById(id).orElseGet(OutBoundOrder::new);
        ValidationUtil.isNull(outBoundOrder.getId(),"OutBoundOrder","id",id);
        return outBoundOrderMapper.toDto(outBoundOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(OutBoundOrder resources) {
        outBoundOrderRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OutBoundOrder resources) {
        OutBoundOrder outBoundOrder = outBoundOrderRepository.findById(resources.getId()).orElseGet(OutBoundOrder::new);
        ValidationUtil.isNull( outBoundOrder.getId(),"OutBoundOrder","id",resources.getId());
        outBoundOrder.copy(resources);
        outBoundOrderRepository.save(outBoundOrder);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            outBoundOrderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OutBoundOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OutBoundOrderDto outBoundOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编码",  outBoundOrder.getId());
            map.put("订单类型",  outBoundOrder.getOrderType());
            map.put("订单状态",  outBoundOrder.getOrderStatus());
            map.put("工程号", outBoundOrder.getWorkNum());
            map.put("优先级",  outBoundOrder.getPriority());
            map.put("创建时间",  outBoundOrder.getCreateTime());
            map.put("开始时间",  outBoundOrder.getStartTime());
            map.put("结束时间",  outBoundOrder.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional
    public void startOrder(int orderId) {
        outBoundOrderRepository.startOrder(orderId);
    }

    @Override
    @Transactional
    public void finishOrder(int orderId) {
        outBoundOrderRepository.finishOrder(orderId);
    }
}